﻿namespace Vdc.Grid
{
    using System;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    internal class GridHeading : WebControl, INamingContainer
    {
        private Vdc.Grid.Grid grid;

        public GridHeading(Vdc.Grid.Grid _grid)
        {
            this.grid = _grid;
            this.ID = this.GetHeadingID();
        }

        protected override void CreateChildControls()
        {
            this.CssClass = "ob_gHeadingCont " + ((!this.grid.AllowGrouping && !this.grid.displayTopFooter) ? "" : " ob_gHeadingContWG");
            HtmlContainer child = new HtmlContainer();
            HtmlContainer container2 = new HtmlContainer();
            child.CssClass = "ob_gHCLS";
            container2.CssClass = "ob_gHCRS";
            if (this.grid.IsDesignTimeRender && this.grid.UseEmbeddedImages)
            {
                child.Style["background-image"] = "url(" + this.grid.ImagesURL.Grouping_GroupingAreaLeft + ")";
                base.Style["background-image"] = "url(" + this.grid.ImagesURL.Grouping_GroupingArea + ")";
                container2.Style["background-image"] = "url(" + this.grid.ImagesURL.Grouping_GroupingAreaRight + ")";
            }
            child.Text = "&#160;";
            container2.Text = "&#160;";
            string headingTemplateId = this.grid.TemplateSettings.HeadingTemplateId;
            if (!string.IsNullOrEmpty(headingTemplateId))
            {
                if (!(this.grid.Templates.GetById(headingTemplateId) is GridRuntimeTemplate))
                {
                    GridTemplate byId = this.grid.Templates.GetById(headingTemplateId);
                    if (byId == null)
                    {
                        throw new InvalidTemplateIDException(headingTemplateId);
                    }
                    this.Controls.Add(byId);
                    byId.DataBind();
                }
                else
                {
                    GridRuntimeTemplate runtimeTemplateById = this.grid.Templates.GetRuntimeTemplateById(headingTemplateId);
                    if (runtimeTemplateById == null)
                    {
                        throw new InvalidTemplateIDException(headingTemplateId);
                    }
                    this.Controls.Add(runtimeTemplateById);
                    runtimeTemplateById.DataBind();
                }
            }
            this.Controls.Add(child);
            this.Controls.Add(container2);
        }

        internal void ForceEnsureChildControls()
        {
            this.EnsureChildControls();
        }

        protected virtual string GetHeadingID()
        {
            return ("ob_" + this.grid.GridID + "Heading");
        }

        protected override HtmlTextWriterTag TagKey
        {
            get
            {
                return HtmlTextWriterTag.Div;
            }
        }
    }
}

